package com.gaohe.web.controller.scp;

import java.util.List;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.gaohe.common.annotation.Log;
import com.gaohe.common.core.controller.BaseController;
import com.gaohe.common.core.domain.AjaxResult;
import com.gaohe.common.enums.BusinessType;
import com.gaohe.scp.domain.CsSetting;
import com.gaohe.scp.service.ICsSettingService;
import com.gaohe.common.utils.poi.ExcelUtil;
import com.gaohe.common.core.page.TableDataInfo;

/**
 * 系统设置Controller
 *
 * @author liuxiangyu
 * @date 2025-03-25
 */
@RestController
@RequestMapping("/scp/setting")
public class CsSettingController extends BaseController
{
    @Autowired
    private ICsSettingService csSettingService;

    /**
     * 查询系统设置列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:setting:list')")
    @GetMapping("/list")
    public TableDataInfo list(CsSetting csSetting)
    {
        startPage();
        List<CsSetting> list = csSettingService.selectCsSettingList(csSetting);
        return getDataTable(list);
    }
    /**
     * 根据type类型查询   王浩宇
     * */
    @GetMapping("/type/{type}")
    public AjaxResult getSettingByType(@PathVariable String type){
        CsSetting query = new CsSetting();
        query.setType(type);
        CsSetting csSetting = csSettingService.selectCsSettingByType(type);
        return AjaxResult.success(csSetting);
    }

    @GetMapping("/secret")
    public AjaxResult getSettingByType(){
        String s = "7c971672f13bf564a8e86403302ef5bd";
        return AjaxResult.success(s);
    }

    /**
     * 导出系统设置列表
     */
//    @PreAuthorize("@ss.hasPermi('scp:setting:export')")
    @Log(title = "系统设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CsSetting csSetting)
    {
        List<CsSetting> list = csSettingService.selectCsSettingList(csSetting);
        ExcelUtil<CsSetting> util = new ExcelUtil<CsSetting>(CsSetting.class);
        util.exportExcel(response, list, "系统设置数据");
    }

    /**
     * 获取系统设置详细信息
     */
//    @PreAuthorize("@ss.hasPermi('scp:setting:query')")
    @GetMapping(value = "/{settingId}")
    public AjaxResult getInfo(@PathVariable("settingId") Long settingId)
    {
        return success(csSettingService.selectCsSettingBySettingId(settingId));
    }

    /**
     * 管理端
     * 新增系统设置
     */
//    @PreAuthorize("@ss.hasPermi('scp:setting:add')")
    @Log(title = "系统设置", businessType = BusinessType.INSERT)
    @PostMapping("/gld")
    public AjaxResult addSetting(@RequestBody CsSetting csSetting)
    {
        String content = csSetting.getContent();
        if (content==null){
            return error("请输入内容");
        }
        int i = csSettingService.insertCsSetting(csSetting);
        return toAjax(1);
    }

    /**
     * 修改系统设置
     */
//    @PreAuthorize("@ss.hasPermi('scp:setting:edit')")
    @Log(title = "系统设置", businessType = BusinessType.UPDATE)
    @PutMapping("/gld")
    public AjaxResult edit(@RequestBody CsSetting csSetting)
    {
        String content = csSetting.getContent();
        String s = StringUtils.substringBetween(content, "<p>", "</p>");
        if (s.equals("<br>")  || s.isBlank()){
            return error("请输入内容");
        }
        csSettingService.updateCsSetting(csSetting);
        return toAjax(1);
    }

    /**
     * 删除系统设置
     */
//    @PreAuthorize("@ss.hasPermi('scp:setting:remove')")
    @Log(title = "系统设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{settingIds}")
    public AjaxResult remove(@PathVariable Long[] settingIds)
    {
        csSettingService.deleteCsSettingBySettingIds(settingIds);
        return toAjax(1);
    }
}
